/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entities.Account;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY_THI
 */
@ManagedBean
@RequestScoped
public class AddAccountBean {

    @EJB
    private AddAccountLocal addAccount;
    @EJB
    private CheckAccountLocal checkAccount;
    private String accountCode;
    private String accountName;
    private String address;
    private String identificationCard;

    public String getAccountCode() {
        return accountCode;
    }

    public void setAccountCode(String accountCode) {
        this.accountCode = accountCode;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getIdentificationCard() {
        return identificationCard;
    }

    public void setIdentificationCard(String identificationCard) {
        this.identificationCard = identificationCard;
    }

    /** Creates a new instance of AddAccountBean */
    public AddAccountBean() {
    }

    public String add() {
        if (checkAccount.checkAccount(accountCode)) {
            FacesMessage msg = new FacesMessage("Error", "Account Code is existed.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "addAccount.xhtml";
        } else {
            Account acc = new Account();
            acc.setAcountCode(accountCode);
            acc.setAccountName(accountName);
            acc.setAddress(address);
            acc.setIdentificationCard(identificationCard);
            acc.setBalance(BigDecimal.valueOf(50000));
            acc.setStatus(Boolean.TRUE);
            acc.setPinCode(EncryptMD5.getHashString("123456"));
            boolean result = addAccount.addAccount(acc);
            if (result) {
                FacesMessage msg = new FacesMessage("success", "Add Account successfull! ");
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                return "home.xhtml?faces-redirect=true";
            } else {
                FacesMessage msg = new FacesMessage("Error", "Account Code is existed.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "addAccount.xhtml";
            }
        }
    }
}
